package com.longtop.perform.kh.score.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.SpringBeanFactory;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.kh.score.vo.ScoreParamVO;
import com.longtop.perform.khyg.model.TKhygIndex;
import com.longtop.perform.khyg.model.TKhygSegment;
import com.longtop.perform.khyg.service.ProjectService;

/**
 * 
 * @author 孙景弘
 * 
 */
public class ScoreService extends BaseService {

	public static final String BEAN_ID = "scoreService";

	/**
	 * 获得评分模型List
	 * 
	 * @param ds_score
	 * @return
	 */
	public Dataset getScoreModelList(Dataset ds_score) {
		ds_score = this.queryForPageByIbatis("khScoreSql.queryScoreModel", null, null, ds_score, false);
		Iterator<Record> iterator = ds_score.getRecordList().iterator();
		while (iterator.hasNext()) {
			Record record = iterator.next();
			String code = record.getValue("scoreModeId").toString();
			// 对应的JSP页面名称
			String pagePath = "scoreModel_" + code + ".jsp";
			record.setValue("pagePath", pagePath);
		}
		return ds_score;
	}

	/**
	 * 保存评分模型
	 * 
	 * @param ds_projectIndex
	 * @param ds_scoreParam
	 * @throws Exception
	 */
	public TKhygIndex saveScore(Dataset ds_projectIndex, Dataset ds_scoreParam) throws Exception {
		TKhygIndex projectIndex = BeanUtil.datasetToVO(ds_projectIndex, TKhygIndex.class);
		ScoreParamVO paramVO = BeanUtil.datasetToVO(ds_scoreParam, ScoreParamVO.class);
		String formula = paramVO.getFormula();// 原公式
		// 替换数据后的带参数函数公式：
		formula = paramVO.buildFormulaFromBean();
		projectIndex.setFormula(formula);

		this.update(projectIndex);

		ProjectService projectService = SpringBeanFactory.getBean(ProjectService.BEAN_ID);
		String projectId = projectIndex.getProjectId();
		// 判断该方案定量指标是否所有都已经设置了评分模型：
		List list = this.queryAll("from TKhygIndex i,TZbIndex z where i.indexId = z.indexId and z.indexType != '1' and i.projectId = '" + projectId + "' and i.formula is null");
		boolean flag = list.size() > 0 ? false : true;

		// 更新考核方案-对应阶段的进度状态
		List<TKhygSegment> segmentList = this.queryAll("from TKhygSegment where projectId = '" + projectId + "'");
		if (segmentList.size() > 0) {
			for (TKhygSegment khygSegment : segmentList) {
				projectService.updateSegmentStatus(TKhygSegment.class, khygSegment.getSegmentId(), 2, flag);// 评分模型
				projectService.updateSegmentStatus(TKhygSegment.class, khygSegment.getSegmentId(), 6, false);// （计算成绩为0）
			}
		}

		return projectIndex;
	}

	/**
	 * 装载评分模型/初始化数据
	 * 
	 * @param ds_projectIndex
	 * @param ds_scoreParam2
	 * @throws Exception
	 */
	public ScoreParamVO loadScore(Dataset ds_projectIndex) throws Exception {
		ScoreParamVO paramVO = new ScoreParamVO();
		TKhygIndex projectIndex = BeanUtil.datasetToVO(ds_projectIndex, TKhygIndex.class);

		// 判断符合条件的index是否已已经存在评分模型：
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("projectId", projectIndex.getProjectId());
		queryMap.put("indexId", projectIndex.getIndexId());
		queryMap.put("scoreModeId", projectIndex.getScoreModeId());
		List list = this.queryForListByIbatis("khScoreSql.queryProjectIndex", queryMap);

		if (list.size() <= 0) {// 若不存在符合条件的index返回NULL
			return null;
		}

		// 存在符合条件的index则加载数据:
		String formula = projectIndex.getFormula();// 指标公式
		paramVO = paramVO.buildBeanFromFormula(formula);// 替换公式到bean

		return paramVO;

	}

	/**
	 * 计算成绩
	 * 
	 * @param segmentId
	 * @return
	 */
	public String countScore(String segmentId) throws Exception {

		ProjectService projectService = SpringBeanFactory.getBean(ProjectService.BEAN_ID);
		boolean flag = projectService.getSegmentStatus(TKhygSegment.class, segmentId, 2);
		return "待开发。";
	}
}